package com.school.empsys.controller;

import java.util.List;

import com.school.empsys.utils.UserUtil;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.google.common.collect.Maps;
import com.school.empsys.mapper.RoleMapper;
import com.school.empsys.dto.RoleDto;
import com.school.empsys.model.Role;
import com.school.empsys.page.table.PageTableRequest;
import com.school.empsys.page.table.PageTableHandler;
import com.school.empsys.page.table.PageTableResponse;
import com.school.empsys.page.table.PageTableHandler.CountHandler;
import com.school.empsys.page.table.PageTableHandler.ListHandler;
import com.school.empsys.service.RoleService;

/**
 * 角色相关接口
 */
@RestController
@RequestMapping("/roles")
public class RoleController {

	@Autowired
	private RoleService roleService;
	@Autowired
	private RoleMapper roleDao;

	/**
	 * 保存角色
	 * @param roleDto
	 */
	@PostMapping
	@RequiresPermissions("sys:role:add")
	public void saveRole(@RequestBody RoleDto roleDto) {
		roleService.saveRole(roleDto);
	}

	/**
	 * 角色列表
	 * @param request
	 * @return
	 */
	@GetMapping
	@RequiresPermissions("sys:role:query")
	public PageTableResponse listRoles(PageTableRequest request) {
		return new PageTableHandler(new CountHandler() {

			@Override
			public int count(PageTableRequest request) {
				return roleDao.count(request.getParams());
			}
		}, new ListHandler() {

			@Override
			public List<Role> list(PageTableRequest request) {
				List<Role> list = roleDao.list(request.getParams(), request.getOffset(), request.getLimit());
				return list;
			}
		}).handle(request);
	}

	/**
	 * 根据id获取角色
	 * @param id
	 * @return
	 */
	@GetMapping("/{id}")
	@RequiresPermissions("sys:role:query")
	public Role get(@PathVariable Long id) {
		return roleDao.getById(id);
	}

	/**
	 * 所有角色
	 * @return
	 */
	@GetMapping("/all")
	@RequiresPermissions(value = { "sys:user:query", "sys:role:query" }, logical = Logical.OR)
	public List<Role> roles() {
		List<Role> list = roleDao.list(Maps.newHashMap(), null, null);
		if (UserUtil.hasRole(com.school.empsys.constants.Role.ROLE_EMP_ADMIN.getName())) {
			list.removeIf(x -> com.school.empsys.constants.Role.ROLE_EMP_ADMIN.getName().equals(x.getName())
				|| com.school.empsys.constants.Role.ROLE_ADMIN.getName().equals(x.getName()));
		}
		return list;
	}

	/**
	 * 根据用户id获取拥有的角色
	 * @param userId
	 * @return
	 */
	@GetMapping(params = "userId")
	@RequiresPermissions(value = { "sys:user:query", "sys:role:query" }, logical = Logical.OR)
	public List<Role> roles(Long userId) {
		return roleDao.listByUserId(userId);
	}

	/**
	 * 删除角色
	 * @param id
	 */
	@DeleteMapping("/{id}")
	@RequiresPermissions(value = { "sys:role:del" })
	public void delete(@PathVariable Long id) {
		roleService.deleteRole(id);
	}
}
